import { createRouter, createWebHashHistory } from "vue-router";
import menuMap from "./menuMap";

const routerMap = [];
function setRouter(data) {
  for (let i = 0; i < data.length; i++) {
    const element = data[i];
    routerMap.push({
      path: element.path,
      name: element.name,
      component: element.component,
      meta: element.meta
    });
    if (element.children) {
      setRouter(element.children);
    }
  }
}
setRouter(menuMap);
const routes = [
  {
    path: "/",
    name: "base",
    component: () => import("../views/layout/Layout.vue"),
    children: [
      ...routerMap,
      ...[
        { path: "/:pathMatch(.*)", component: () => import("../views/error/404.vue") },
        { path: "/404", component: () => import("../views/error/404.vue") },
      ],
    ],
  },
  // {
  //   path: "/Login",
  //   name: "登录",
  //   component: () => import("../views/login/Login.vue"),
  // },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path === "/") {
    next({ path: "/Home" });
  } else {
    next();
  }
});
export default router;
